<template>
	<view class="lz-applet">
		<view class="foot-center">
			<u-search placeholder="搜索小程序" shape="square" :show-action="false" height="80"></u-search>
			<view class="f-c-center">
				<view class="c-title">
					<text>最近使用的小程序</text>
					<view>更多 <u-icon name="arrow-right" size="28"></u-icon>
					</view>
				</view>
				<view class="ul">
					<view class="li" @click="download('__UNI__01F8D0C','https://vkceyugu.cdn.bspapp.com/VKCEYUGU-2a2d5041-c907-4174-9bb4-b78545d83f43/faf9a440-9997-4ec1-8889-9f44a77e460d.wgt')">
						<u-avatar src="../../static/image/applet/__UNI__01F8D0C.png"></u-avatar>
						<text class="text">印签查</text>
					</view>
					<view class="li" @click="download('__UNI__9CA02B3','https://vkceyugu.cdn.bspapp.com/VKCEYUGU-2a2d5041-c907-4174-9bb4-b78545d83f43/f6f260ce-401f-41d7-b52f-e49b37a015c2.wgt')">
						<u-avatar src="../../static/image/applet/__UNI__9CA02B3.png"></u-avatar>
						<text class="text">办公助手</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
let that
//#ifdef APP-PLUS
const mp = uni.requireNativePlugin('uniMP');
//#endif
import {
	mapGetters,
	mapActions
} from 'vuex'
export default {
	computed:{
		...mapGetters(['APPTheme', 'userInfo', 'api'])
	},
	data() {
		return {
			applet:{
				appid: '__UNI__9CA02B3',
				wgt: '/static/applet/__UNI__9CA02B3.wgt'
			}
		}
	},
	mounted() {
		that=this
	},
	methods: {
		//下载资源
		download(appid,url){
			this.applet.appid=appid
			uni.getSavedFileList({
				success:function(data){
					//console.log(data);
					data.fileList.forEach((i)=>{
						uni.removeSavedFile({filePath:i.filePath})
					})
				}
			})
			
			//文件下载
			uni.downloadFile({
				url: url,
				success: function(res) {
					//保存文件到本地
					uni.saveFile({
						tempFilePath: res.tempFilePath,
						success: function(item) {
							//console.log(item.savedFilePath);
							var p = plus.io.convertLocalFileSystemURL(item.savedFilePath);
							//console.log(p);
							that.applet.wgt=p
							that.installApplet()
						}
					})
					
				}
			});
		},
		
		installApplet(appid,url) {
			this.applet.appid=appid
			this.applet.wgt=`/static/applet/${appid}.wgt`
			that.$emit('goback',true)
			mp.getUniMPVersion(this.applet.appid, (ret) => {
				if (0 != ret.code) { //获取失败时安装应用
					mp.installUniMP({
						appid: this.applet.appid,
						wgtFile: this.applet.wgt
					}, (r) => {
						if (0 == r.code) {
							//console.log('安装成功');
							that.openMP()
						} else {
							uni.showModal({
								title: '安装失败',
								content: JSON.stringify(r),
								showCancel: false
							});
						}
						//console.log('installUniMP: ' + JSON.stringify(r));
					});
				} else {
					//console.log('已安装');
					that.openMP()
				}
				//console.log('getUniMPVersion: ' + JSON.stringify(ret));
			});
		},
		openMP() {
			mp.openUniMP({
				appid: this.applet.appid,
				icon:`/static/image/applet/${this.applet.appid}.png`
			}, (ret) => {
				if (0 != ret.code) {
					uni.showModal({
						title: '启动失败',
						content: JSON.stringify(ret),
						showCancel: false
					});
				}
				//console.log('openUniMP: ' + JSON.stringify(ret));
			});
		},
	}
}
</script>

<style scoped lang="scss">
	//二楼
	.foot-center {
		padding: 80rpx 80rpx 0;
		box-sizing: border-box;
	
		.f-c-center {
			.c-title {
				display: flex;
				justify-content: space-between;
				margin: 40rpx 0;
				color: #888;
				font-size: 28rpx;
			}
	
			.ul {
				display: flex;
				flex-wrap: wrap;
	
				.li {
					width: 126rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					color: #00aaff;
					margin-right: 20rpx;
					margin-bottom: 20rpx;
	
					.text {
						margin-top: 3px;
						font-size: 28rpx;
					}
				}
			}
		}
	}
</style>
